package com.example.smartplay.controller;


import com.example.smartplay.entity.Apply;
import com.example.smartplay.entity.Clubmembership;
import com.example.smartplay.entity.Result;
import com.example.smartplay.mapper.ApplyMapper;
import com.example.smartplay.mapper.ClubMapper;
import com.example.smartplay.mapper.ClubmembershipMapper;
import com.example.smartplay.service.ApplyService;
import com.example.smartplay.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private ApplyService applyService;
    @Autowired
    private ClubmembershipMapper clubmembershipMapper;
    //分页查询社团申请
    @GetMapping("/getApply/{pageNum}/{pageSize}")
    public Result getApply(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    )
    {

        System.out.println("pageNum = " + pageNum);
        System.out.println("pageSize = " + pageSize);
        return applyService.getApply(pageNum,pageSize);
    }
    //分页查询社团申请
    @GetMapping("/getApplys/{pageNum}/{pageSize}")
    public Result getApplys(
            @RequestParam(required = false) String status,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){ // 查询总记录数
        int total = applyMapper.countapplys();

        // 查询分页数据
        List<Apply> apply = applyMapper.selectapplyWithName(status, pageNum,pageSize);

        // 构建返回结果，包括分页数据和总记录数
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("data", apply);

        return Result.success(result);

    }


    //新增申请
    @PostMapping("/save")
    public Result save(@RequestBody Apply apply)
    {
        //利用token获得学生id
        Map<String, Object> userinfo = ThreadLocalUtil.get();
        Integer studentId = (Integer)userinfo.get("id");
        //添加日志查看studentid 的值
        System.out.println("studentId = " + studentId);

        apply.setStudentId(studentId);
        //从审核状态先查看学生是否申请过该社团
       List<Apply> list =  applyMapper.selectByStatus(apply.getStudentId(),apply.getClubId());
        if (list != null && !list.isEmpty()){
            return Result.error("该学生已经申请过该社团");
        }
        //从成员列表里查看学生是否属于该社团
        List<Clubmembership> list1 = clubmembershipMapper.selectByClubId(apply.getClubId(),apply.getStudentId());
        if (list1 != null && !list1.isEmpty()){
            return Result.error("该学生已经是该社团成员");
        }

        apply.setStatus("待审核");

       applyService.save(apply);


       return Result.success();
    }
    //修改申请
    @PutMapping("/update")
    public Result update(@RequestBody Apply apply)
    {
        applyService.updateById(apply);
        if (apply.getStatus().equals("审核通过")){


            //判断是否是社团成员
            List<Clubmembership> list = clubmembershipMapper.selectByClubId(apply.getClubId(),apply.getStudentId());
            if (list != null && !list.isEmpty()){
                return Result.error("该学生已经是该社团成员");
            }
            //添加社团成员
            Clubmembership clubmembership = new Clubmembership();
            clubmembership.setClubId(apply.getClubId());
            clubmembership.setStudentId(apply.getStudentId());
            clubmembershipMapper.insert(clubmembership);
        }

        return Result.success();
    }
    //删除申请
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable List<Integer> ids)
    {
        applyService.removeByIds(ids);
        return Result.success();
    }

}
